package com.dogdog.activity;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.android.volley.AuthFailureError;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.dogdog.bean.Status;
import com.dogdog.manager.C;
import com.dogdog.manager.MyApplication;
import com.dogdog.util.ToastTool;
import com.google.gson.Gson;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class Hand_registered extends BaseActivity implements OnClickListener {
	//组件区
	private TextView left_btn;
	private TextView right_btn;
	private TextView title;
	private EditText et_account,et_pwd,et_email,et_confirm_pwd;
	private static final String[] sex = {"保密" ,"男", "女"}; 
	private Spinner sp_sex;
	private int sexNumber ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.person_registered_one);
		initView(); 
	}

	private void initView() {
		title = (TextView)findViewById(R.id.tv_title);
		title.setText("注册");
		left_btn = (TextView)findViewById(R.id.imgv_left_btn);
		left_btn.setVisibility(View.VISIBLE);
		left_btn.setText("登录");
        left_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		       finish();	
			}});
		right_btn = (TextView)findViewById(R.id.tv_right);
		right_btn.setText("完成");
		right_btn.setOnClickListener(this);
		et_account = (EditText)findViewById(R.id.et_account);
		et_email = (EditText)findViewById(R.id.et_email);
		et_pwd = (EditText)findViewById(R.id.et_pwd);
		et_confirm_pwd=(EditText)findViewById(R.id.et_confirm_pwd);
		sp_sex = (Spinner)findViewById(R.id.sp_sex);
		SpinnerAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item , sex);
		sp_sex.setAdapter(adapter);
		sp_sex.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			 sexNumber =arg2;
			 //arg2=0 表示性别保密，1表示男性，2表示女性，默认为保密
			    //设置显示当前选择的项
			    arg0.setVisibility(View.VISIBLE);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) { }
		});
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			right_btn.setEnabled(false);
			registeredRequest();
			break;
		}
	}

	public boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}
    
	/***
	 * 网络请求区域  
	 * **/
    //注册请求
	public void registeredRequest(){
		if (et_pwd.getText().toString().length()< 6 ||et_pwd.getText().toString().length()> 16) {
			ToastTool.show(Hand_registered.this,"密码长度不符合要求");
		} else if (!isEmail(et_email.getText().toString())) {
			ToastTool.show(Hand_registered.this,"请输入正确的邮箱格式");
		} else if(!et_pwd.getText().toString().equals(et_confirm_pwd.getText().toString())){
			ToastTool.show(Hand_registered.this,"两次输入的密码不一致");
		}else {
		StringRequest stringRequest = new StringRequest(Method.POST,C.URL.RegisteredURL,  
                new Response.Listener<String>() {  
                    @Override  
                    public void onResponse(String arg) {   
                        Gson gson = new Gson();
                        Status status = gson.fromJson(arg, Status.class); 
                        if(status.getState()==1){
                        ToastTool.show(Hand_registered.this,"注册成功");
                        finish();
                       }else{
                    	ToastTool.status(Hand_registered.this,status);}
                    }  
                }, new Response.ErrorListener() {  
                    @Override  
                    public void onErrorResponse(VolleyError error) {  
                        Log.e("TAG", error.getMessage(), error);  
                    	ToastTool.show(Hand_registered.this,error.toString());
                    }  
                }){
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
			Map<String,String> params=new HashMap<String,String>();
				params.put("name", et_account.getText().toString());
				params.put("pass", et_pwd.getText().toString());
				params.put("sex", Integer.toString(sexNumber));
				params.put("email", et_email.getText().toString());
				return params; 
			}
		};  
		MyApplication.getHttpQueue().add(stringRequest);
		}
	}
	
}
